package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.modules.dic.entity.DicBeGoodAt;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterAccount;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterHospital;
import org.jeecg.common.modules.doctor.mapper.DoctorRegisterHospitalMapper;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterHospitalService;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterHospitalVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: doctor_register_hospital
 * @Author: jeecg-boot
 * @Date:   2020-12-18
 * @Version: V1.0
 */
@Slf4j
@Service
public class DoctorRegisterHospitalServiceImpl extends ServiceImpl<DoctorRegisterHospitalMapper, DoctorRegisterHospital> implements IDoctorRegisterHospitalService {
    @Resource
    private DoctorRegisterHospitalMapper doctorRegisterHospitalMapper;

    @Override
    public List<DoctorRegisterHospitalVo> getInfoList(Integer dataStart, Integer pageSize){
        List<DoctorRegisterHospital> doctorRegisterHospitals = doctorRegisterHospitalMapper.getInfoList(dataStart,pageSize);
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        List<DoctorRegisterHospitalVo> doctorRegisterHospitalVos = new ArrayList<>();
        String createTime = "";
        String updateTime = "";
        //复制对象且转换日期到字符串
        for(DoctorRegisterHospital doctorRegisterHospital:doctorRegisterHospitals){
            DoctorRegisterHospitalVo doctorRegisterHospitalVo = new DoctorRegisterHospitalVo();
            BeanUtils.copyProperties(doctorRegisterHospital,doctorRegisterHospitalVo);

            if(Objects.nonNull(doctorRegisterHospital.getCreateTime())){
                createTime = sdf.format(doctorRegisterHospital.getCreateTime());
                doctorRegisterHospitalVo.setCreateTime(createTime);
            }
            if(Objects.nonNull(doctorRegisterHospital.getUpdateTime())){
                updateTime = sdf.format(doctorRegisterHospital.getUpdateTime());
                doctorRegisterHospitalVo.setUpdateTime(updateTime);
            }

            doctorRegisterHospitalVos.add(doctorRegisterHospitalVo);
        }
        return doctorRegisterHospitalVos;
    }

    @Override
    public int getInfoListCount(){
        return doctorRegisterHospitalMapper.getInfoListCount();
    }

    @Override
    public List<DoctorRegisterHospital> getAll(){
        LambdaQueryWrapper<DoctorRegisterHospital> queryWrapper = Wrappers.lambdaQuery();
        return list(queryWrapper);
    }

    @Override
    public List<DicBeGoodAt> getRegisterDepartment(){
        return doctorRegisterHospitalMapper.getRegisterDepartment();
    }

    @Override
    public List<DoctorRegisterOrderVo> getHospitalOrder(String hospitalId,String recordId,Integer dataStart, Integer pageSize){
        List<DoctorRegisterAccount> doctorRegisterAccounts = doctorRegisterHospitalMapper.getHospitalList(hospitalId,recordId,dataStart,pageSize);
        log.info("数据表查询的数据：{}",doctorRegisterAccounts);
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        List<DoctorRegisterOrderVo> doctorRegisterOrderList = new ArrayList<>();
        String createTime = "";
        //复制对象且转换日期到字符串
        for(DoctorRegisterAccount doctorRegisterAccount:doctorRegisterAccounts){
            createTime = sdf.format(doctorRegisterAccount.getCreateTime());
            DoctorRegisterOrderVo doctorRegisterOrderVo = new DoctorRegisterOrderVo();
            BeanUtils.copyProperties(doctorRegisterAccount,doctorRegisterOrderVo);
            doctorRegisterOrderVo.setCreateTime(createTime);
            doctorRegisterOrderList.add(doctorRegisterOrderVo);
        }
        log.info("处理后的数据：{}",doctorRegisterAccounts);
        return doctorRegisterOrderList;
    }

    @Override
    public Integer getHospitalOrderCount(String hospitalId,String recordId){
        return doctorRegisterHospitalMapper.getHospitalOrderCount(hospitalId,recordId);
    }

    /**
     * 日期转字符串
     * @param date
     * @return
     */
    public String dateToStr(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        return sdf.format(date);
    }
}
